package com.ruoyi.framework.web.service;//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import cn.xfyun.base.webscoket.WebSocketClient;
import cn.xfyun.service.tts.AbstractTtsWebSocketListener;
import com.google.gson.JsonObject;
import okhttp3.OkHttpClient;
import okhttp3.WebSocket;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
import java.security.SignatureException;
import java.util.Base64;

public class MyTtsClient extends WebSocketClient {
    private static final String MINOR_LANGUAGE = "UNICODE";
    private static final String MP3 = "lame";
    private String aue;
    private Integer sfl;
    private String auf;
    private String vcn;
    private Integer speed;
    private Integer volume;
    private Integer pitch;
    private Integer bgs;
    private String tte;
    private String reg;
    private String rdn;
    private String ent;
    private String ttp;

    public MyTtsClient(MyTtsClient.Builder builder) {
        this.okHttpClient = (new OkHttpClient()).newBuilder().build();
        this.originHostUrl = builder.hostUrl;
        this.appId = builder.appId;
        this.apiKey = builder.apiKey;
        this.apiSecret = builder.apiSecret;
        this.aue = builder.aue;
        this.sfl = builder.sfl;
        this.auf = builder.auf;
        this.vcn = builder.vcn;
        this.speed = builder.speed;
        this.volume = builder.volume;
        this.pitch = builder.pitch;
        this.bgs = builder.bgs;
        this.tte = builder.tte;
        this.reg = builder.reg;
        this.rdn = builder.rdn;
        this.ent = builder.ent;
//        this.ttp = builder.ttp;
    }

    @Override
    public String getAppId() {
        return this.appId;
    }

    @Override
    public String getApiKey() {
        return this.apiKey;
    }

    @Override
    public String getApiSecret() {
        return this.apiSecret;
    }

    public String getOriginHostUrl() {
        return this.originHostUrl;
    }

    public String getAue() {
        return this.aue;
    }

    public Integer getSfl() {
        return this.sfl;
    }

    public String getAuf() {
        return this.auf;
    }

    public String getVcn() {
        return this.vcn;
    }

    public Integer getSpeed() {
        return this.speed;
    }

    public Integer getVolume() {
        return this.volume;
    }

    public Integer getPitch() {
        return this.pitch;
    }

    public Integer getBgs() {
        return this.bgs;
    }

    public String getTte() {
        return this.tte;
    }

    public String getReg() {
        return this.reg;
    }

    public String getRdn() {
        return this.rdn;
    }

    public String getEnt() {
        return this.ent;
    }
    public String getTtp() {
        return this.ttp;
    }

    public OkHttpClient getClient() {
        return this.okHttpClient;
    }

    @Override
    public WebSocket getWebSocket() {
        return this.webSocket;
    }

    public void send(String text, AbstractTtsWebSocketListener webSocketListener,int speed) throws UnsupportedEncodingException, MalformedURLException, SignatureException {
        this.createWebSocketConnect(webSocketListener);
        if ("UNICODE".equals(this.tte)) {
            text = Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_16LE));
        } else {
            text = Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8));
        }

        JsonObject frame = new JsonObject();
        JsonObject common = new JsonObject();
        JsonObject business = new JsonObject();
        JsonObject data = new JsonObject();
        common.addProperty("app_id", this.appId);
        business.addProperty("aue", this.aue);
        business.addProperty("sfl", this.sfl);
        business.addProperty("auf", this.auf);
        business.addProperty("vcn", this.vcn);
        if (speed>0) {
            business.addProperty("speed", speed);
        } else {
            business.addProperty("speed", this.speed);
        }
        business.addProperty("volume", this.volume);
        business.addProperty("pitch", this.pitch);
        business.addProperty("bgs", this.bgs);
        business.addProperty("tte", this.tte);
        business.addProperty("reg", this.reg);
        business.addProperty("rdn", this.rdn);
//        business.addProperty("ent", this.ent);
        business.addProperty("ent", this.ent);
        business.addProperty("ttp", "cssml");
//        businesssetParameter(SpeechConstant.TEXT_ENCODING, "GB2312");
        data.addProperty("text", text);
        data.addProperty("status", 2);
        frame.add("common", common);
        frame.add("business", business);
        frame.add("data", data);
        this.webSocket.send(frame.toString());
    }

    public static final class Builder {
        private String hostUrl = "https://tts-api.xfyun.cn/v2/tts";
//        private String hostUrl = "https://api.xfyun.cn/v1/service/v1/tts";
        private String appId;
        private String apiKey;
        private String apiSecret;
        private String aue = "lame";
        private Integer sfl = 1;
        private String auf = "audio/L16;rate=16000";
        private String vcn = "xiaoyan";
        private Integer speed = 50;
        private Integer volume = 50;
        private Integer pitch = 50;
        private Integer bgs = 0;
        private String tte = "UTF8";
        private String reg = "0";
        private String rdn = "0";
        private String ent = "intp65";
//        private String ttp = "cssml";

        public Builder() {
        }

        public MyTtsClient build() throws MalformedURLException, SignatureException {
            return new MyTtsClient(this);
        }

        public MyTtsClient.Builder signature(String appId, String apiKey, String apiSecret) {
            this.appId = appId;
            this.apiKey = apiKey;
            this.apiSecret = apiSecret;
            return this;
        }

        public MyTtsClient.Builder hostUrl(String hostUrl) {
            this.hostUrl = hostUrl;
            return this;
        }

        public MyTtsClient.Builder aue(String aue) {
            this.aue = aue;
            return this;
        }

        public MyTtsClient.Builder sfl(Integer sfl) {
            if ("lame".equals(this.aue)) {
                this.sfl = 1;
            } else {
                this.sfl = sfl;
            }

            return this;
        }

        public MyTtsClient.Builder auf(String auf) {
            this.auf = auf;
            return this;
        }

        public MyTtsClient.Builder vcn(String vcn) {
            this.vcn = vcn;
            return this;
        }

        public MyTtsClient.Builder speed(Integer speed) {
            this.speed = speed;
            return this;
        }

        public MyTtsClient.Builder volume(Integer volume) {
            this.volume = volume;
            return this;
        }

        public MyTtsClient.Builder pitch(Integer pitch) {
            this.pitch = pitch;
            return this;
        }

        public MyTtsClient.Builder bgs(Integer bgs) {
            this.bgs = bgs;
            return this;
        }

        public MyTtsClient.Builder tte(String tte) {
            this.tte = tte;
            return this;
        }

        public MyTtsClient.Builder reg(String reg) {
            this.reg = reg;
            return this;
        }

        public MyTtsClient.Builder rdn(String rdn) {
            this.rdn = rdn;
            return this;
        }

        public MyTtsClient.Builder ent(String ent) {
            this.ent = ent;
            return this;
        }

//        public MyTtsClient.Builder ttp(String ttp) {
//            this.ttp = ttp;
//            return this;
//        }


    }
}
